﻿using HuiZe.education.Model;
using HuiZe.education.Resource.Controls;
using HuiZe.education.ViewModel;
using PropertyChanged;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace HuiZe.education.View
{
    /// <summary>
    /// ResourceCenterView.xaml 的交互逻辑
    /// </summary>
    /// 
    public partial class ResourceCenterView : UserControl
    {
        public ResourceCenterViewModel resourceViewModel { get; set; }
        public ObservableCollection<ResourceCenterModel> _Resources { get; set; }
        //public ResourceCenterViewModel ResourceView { get; set; }
        public int SelectedItemId { get; set; }
        public ResourceCenterView()
        {
            InitializeComponent();
            Loaded += LoginView_Loaded;
            tabControl.SelectionChanged += TabControl_SelectionChanged;
            //PART_Media_Host.IsVisibleChanged += PART_Media_Host_IsVisibleChanged;
        }

        int firstId = 0;
        private void LoginView_Loaded(object sender, RoutedEventArgs e)
        {
            _current_id = -1;
            _is_loaded_first = false;
            //CourseType.Children.Clear();
            //PART_Media_Host.CollectCallback = Collect;
            resourceViewModel = DataContext as ResourceCenterViewModel;
            //ResourceView = ResourceViewModel;
            pageManage.LoadDataCallback = LoadResource;
            pageManage2.LoadDataCallback = LoadMyZiYuan;
            resourceViewModel.GetSourceList();
            PART_CoursesCloudList.ItemsSource = resourceViewModel.Resources;

            //_Resources = resourceViewModel.Resources;
            //CustomRadioButton FirstradioButton=null;
            //foreach (var res in _Resources)
            //{
            //    if (firstId<=0)
            //    {
            //        firstId = res.id;
            //    }

            //    ControlTemplate ct = new ControlTemplate(typeof(RadioButton));

            //    CustomRadioButton radioButton = new CustomRadioButton
            //    {
            //        Width = 100,
            //        Height=30,
            //        Margin = new Thickness(20, 5, 0, 0),
            //        HorizontalAlignment = HorizontalAlignment.Left,
            //        Content = res.kind_name,
            //        CornerRadius = new CornerRadius(6),
            //        FontSize = 16,
            //        BorderThickness=new Thickness(0),
            //        Id = res.id,
            //        //IsChecked = true,
            //        NomalBackground =Brushes.White,
            //        NomalForground = Brushes.Black,
            //        SelectedBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#55E4C2")),
            //        SelectedForground = Brushes.White,
            //        Template = (ControlTemplate)FindResource("TxtRadioButtonTemplate"),
            //    };
            //    if (FirstradioButton == null)
            //    {
            //        FirstradioButton = radioButton;
            //    }

            //    radioButton.Click += CustomRadioButton_Click;
            //    //CourseType.Children.Add(radioButton);
            //}
            //FirstradioButton.IsChecked = true;
            //UpdateControl(firstId,"");
            //pageManage.ItemSourceCount = resourceViewModel.pageCount;

            //ResourceViewModel.GetCategoryItemList(firstType);
            //ClassDetail.SelectedIndex = 0;
            //var FileTypeList = ResourceViewModel.GetFileType();
            //FileTypePanel.Children.Clear();
            //foreach (var item in FileTypeList)
            //{
            //    CustomRadioButton radioButton = new CustomRadioButton
            //    {
            //        Width = 84,
            //        Margin = new Thickness(20, 0, 0, 0),
            //        HorizontalAlignment = HorizontalAlignment.Left,
            //        Content = item.name,
            //        CornerRadius = new CornerRadius(6),
            //        FontSize = 16,
            //        //IsChecked = true,
            //        NomalBackground = Brushes.White,
            //        NomalForground = Brushes.Black,
            //        SelectedBackground = Brushes.White,
            //        SelectedForground = Brushes.Blue,
            //        Template = (ControlTemplate)FindResource("TxtRadioButtonTemplate"),
            //    };
            //    radioButton.Click += CustomRadioButton_Click_1;
            //    FileTypePanel.Children.Add(radioButton);
            //}
        }

        void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (tabControl.SelectedIndex == 1)
            {
                LoadMyZiYuan();
            }
            
            e.Handled = true;
        }

        //void PART_Media_Host_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        //{
        //    if (PART_Media_Host.Visibility == Visibility.Visible)
        //        PART_Main.Visibility = Visibility.Collapsed;
        //    else
        //        PART_Main.Visibility = Visibility.Visible;
        //}

        private void CustomRadioButton_Click(object sender, RoutedEventArgs e)
        {
            ResourceCenterViewModel ResourceViewModel = DataContext as ResourceCenterViewModel;

            CustomRadioButton radioButton = sender as CustomRadioButton;

            if (radioButton.IsChecked == true)
            {
                string type = radioButton.Content == null ? "" : radioButton.Content.ToString();
                //ResourceViewModel.GetCategoryItemList(type);
                SelectedItemId = radioButton.Id;
                UpdateControl(radioButton.Id, type);
            }

            //try
            //{
            //    CustomRadioButton btn = sender as CustomRadioButton;
            //    string content = btn.Content.ToString();
            //    UpdateControl(SelectedItemId, content);
            //}
            //catch (Exception ex)
            //{

            //}
        }

        private void ClassDetail_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ResourceCenterViewModel ResourceViewModel = DataContext as ResourceCenterViewModel;
            //respResourceDetailModel Detail = ClassDetail.SelectedItem as respResourceDetailModel;
            //if (Detail!=null)
            //{
            //    int id = Detail.kind_item_id;
            //    SelectedItemId = id;
            //    //ResourceViewModel.GetCategoryCourseList(id,"全部");
            //    btnAll_Click(null,null);
            //    UpdateControl(SelectedItemId, "全部");
            //}
        }

        private void CustomRadioButton_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                CustomRadioButton btn = sender as CustomRadioButton;
                string content = btn.Content.ToString();
                UpdateControl(SelectedItemId, content);
            }
            catch (Exception ex)
            {

            }
        }

        private void UpdateControl(int id, string content)
        {
            ResourceCenterViewModel ResourceViewModel = DataContext as ResourceCenterViewModel;
            if (id <= 0)
            {

                id = firstId;

            }
            var list = ResourceViewModel.GetKindItemList(id, content);

            if (list != null)
            {
                //ImageList.Children.Clear();
                foreach (var item in list)
                {

                    //WrapPanel stack = new WrapPanel
                    //{
                    //    Width = 120,
                    //    Height = 180,
                    //    Orientation = Orientation.Vertical,
                    //    HorizontalAlignment = HorizontalAlignment.Left,
                    //    VerticalAlignment = VerticalAlignment.Top
                    //};
                    //Image image = new Image
                    //{
                    //    Source = new BitmapImage(new Uri(item.image)),
                    //    //Name= item.name,
                    //    Width = 120,
                    //    Height = 150,
                    //    HorizontalAlignment = HorizontalAlignment.Left,
                    //    VerticalAlignment = VerticalAlignment.Top

                    //};
                    //WrapPanel stack2 = new WrapPanel
                    //{
                    //    Width = 120,
                    //    Height = 30,
                    //    Orientation = Orientation.Horizontal,
                    //    HorizontalAlignment = HorizontalAlignment.Left,
                    //    VerticalAlignment = VerticalAlignment.Top, 
                    //};
                    //Label lb = new Label
                    //{
                    //    Width = 60,
                    //    Height = 40,
                    //    Content = item.resources_name,
                    //    HorizontalAlignment = HorizontalAlignment.Left
                    //};
                    //Button btn = new Button
                    //{
                    //    Width = 60,
                    //    Height = 40,
                    //    Content = "收藏",
                    //    Foreground = Brushes.Black,
                    //    Background = Brushes.White,
                    //};



                    //stack2.Children.Add(lb);
                    //stack2.Children.Add(btn);
                    //stack.Children.Add(image);
                    //stack.Children.Add(stack2);
                    ResourceCenter book = new ResourceCenter();
                    book.NomalImage = item.image;
                    book.Id = item.id.ToString();
                    book.Labtext = item.kind_item_name;
                    book.Margin = new Thickness(10, 10, 0, 0);
                    book.Height = 200;
                    book.Width = 200;
                    if (item.collection == 0)
                    {
                        book.IsShare = false;
                        book.shareImg.ToolTip = "收藏";
                    }
                    else
                    {
                        book.IsShare = true;
                        book.shareImg.ToolTip = "已收藏";
                    }
                    //book.shareImg.MouseLeftButtonDown += ShareImg_MouseDown;
                    //ImageList.Children.Add(book);
                }
            }
        }

        private void CustomRadioButton_Click_2(object sender, RoutedEventArgs e)
        {
            ResourceCenterViewModel ResourceViewModel = DataContext as ResourceCenterViewModel;
            CustomRadioButton btn = sender as CustomRadioButton;
            string content = btn.Content.ToString();
            UpMyCollection(content, 1);
            pageManage.ItemSourceCount = ResourceViewModel.pageCount;
            //var list = ResourceViewModel.GetMySourceListList(content);
            //if (list != null)
            //{
            //    myImageList.Children.Clear();
            //    foreach (var item in list)
            //    {

            //        //StackPanel stack = new StackPanel
            //        //{

            //        //    Width = 100,
            //        //    Height = 150,
            //        //    HorizontalAlignment = HorizontalAlignment.Left

            //        //};
            //        //Image image = new Image
            //        //{
            //        //    Source = new BitmapImage(new Uri(item.file)),
            //        //    //Name= item.name,
            //        //    Width = 100,
            //        //    Height = 120

            //        //};
            //        //Label lb = new Label
            //        //{
            //        //    Content = item.resources_name,
            //        //    HorizontalAlignment = HorizontalAlignment.Center
            //        //};
            //        //stack.Children.Add(image);
            //        //stack.Children.Add(lb);
            //        //stack.Children.Add(stack2);
            //        BookEdit2 book = new BookEdit2();
            //        book.NomalImage = item.image;
            //        book.Id = item.id.ToString();
            //        book.Labtext = item.resources_name;
            //        book.Margin = new Thickness(10, 10, 0, 0);
            //        book.Height = 200;
            //        book.Width = 200;
            //        if (item.collection == 0)
            //        {
            //            book.IsShare = false;
            //            book.shareImg.ToolTip = "收藏";
            //        }
            //        else
            //        {
            //            book.IsShare = true;
            //            book.shareImg.ToolTip = "已收藏";
            //        }
            //        //book.shareImg.MouseLeftButtonDown += ShareImg_MouseDown;
            //        myImageList.Children.Add(book);
            //    }
            //}
        }

        public void UpMyCollection(string type, int pages)
        {
            ResourceCenterViewModel ResourceViewModel = DataContext as ResourceCenterViewModel;

            string content = type;
            var list = ResourceViewModel.GetMySourceListList(pages);
            if (list != null)
            {
                //myImageList.Children.Clear();
                foreach (var item in list)
                {

                    //StackPanel stack = new StackPanel
                    //{

                    //    Width = 100,
                    //    Height = 150,
                    //    HorizontalAlignment = HorizontalAlignment.Left

                    //};
                    //Image image = new Image
                    //{
                    //    Source = new BitmapImage(new Uri(item.file)),
                    //    //Name= item.name,
                    //    Width = 100,
                    //    Height = 120

                    //};
                    //Label lb = new Label
                    //{
                    //    Content = item.resources_name,
                    //    HorizontalAlignment = HorizontalAlignment.Center
                    //};
                    //stack.Children.Add(image);
                    //stack.Children.Add(lb);
                    //stack.Children.Add(stack2);
                    ResourceCenter book = new ResourceCenter();
                    book.NomalImage = item.ziyuan.image;
                    book.Id = item.id.ToString();
                    book.Labtext = item.ziyuan.kind_item_name;
                    book.Margin = new Thickness(10, 10, 0, 0);
                    book.Height = 200;
                    book.Width = 200;
                    if (item.collection == 0)
                    {
                        book.IsShare = false;
                        book.shareImg.ToolTip = "收藏";
                    }
                    else
                    {
                        book.IsShare = true;
                        book.shareImg.ToolTip = "已收藏";
                    }
                    //book.shareImg.MouseLeftButtonDown += ShareImg_MouseDown;
                    //myImageList.Children.Add(book);
                }
            }
        }

        private void ShareImg_MouseDown(object sender, MouseButtonEventArgs e)
        {
            ResourceCenterViewModel ResourceViewModel = DataContext as ResourceCenterViewModel;

            var button = sender;
            FrameworkElement __do = sender as FrameworkElement;
            ResourceCenter bookbtn = new ResourceCenter();
            while (__do != null)
            {
                __do = __do.Parent as FrameworkElement;
                if (__do.Parent.ToString() == "HuiZe.education.Resource.Controls.ResourceCenter")
                {
                    //AttendClassViewModel Context = __do.DataContext as ;
                    bookbtn = __do.Parent as ResourceCenter;
                    //ParentView.Update0();
                    break;
                }
            }
            if (!string.IsNullOrWhiteSpace(bookbtn.Id))
            {
                ResourceViewModel.AddCollection(Convert.ToInt32(bookbtn.Id));
                UpdateControl(SelectedItemId, "全部");
            }
        }

        private void btnAll_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                CustomRadioButton btn = sender as CustomRadioButton;

                UpdateControl(SelectedItemId, "全部");
            }
            catch { }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ResourceCenterViewModel ResourceViewModel = DataContext as ResourceCenterViewModel;

            ResourceViewModel.AddCollection(43);
        }

        private void TabControl_MouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void TabItem_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            UpMyCollection("全部", 1);
            pageManage2.ItemSourceCount = resourceViewModel.pageCount2;

            pageManage2.UpdataPages();
            //PageManager pageManage = new PageManager
            //{
            //    Height = 28,
            //    Margin = new Thickness(0, 700, 1, 0),
            //    HorizontalAlignment = HorizontalAlignment.Right,
            //    Name= "pageManage2",
            //    Width = 600,
            //};
            //pageManage.ItemSourceCount = resourceViewModel.pageCount2;

            //pageManage.PreviewMouseUp += pageManage2_PreviewMouseUp;

            //pageManageDockPanel.Children.Add(pageManage);
            //pageManage2 = pageManage;
            //< res:PageManager
            //               Height = "28"
            //              Margin = "0,0,1,0"
            //              HorizontalAlignment = "Right"
            //              Counts = "11"
            //              x: Name = "pageManage2"
            //              DockPanel.Dock = "Bottom"
            //              ItemSourceCount = "23" PreviewMouseUp = "pageManage2_PreviewMouseUp" Width = "394" />
        }

        private void PageManage_PreviewMouseUp_1(object sender, MouseButtonEventArgs e)
        {
            if (e.OriginalSource.ToString().Contains("CustomRadioButton"))
            {
                CustomRadioButton button = e.OriginalSource as CustomRadioButton;
                string index = button.Content.ToString();
                if (index == ">")
                {
                    //index = pageManage.Content.ToString();
                    index = pageManage.CurrentIndex < pageManage.ItemSourceCount ? (pageManage.CurrentIndex + 1).ToString() : pageManage.CurrentIndex.ToString();
                }
                if (index == "<")
                {
                    index = pageManage.CurrentIndex > 1 ? (pageManage.CurrentIndex - 1).ToString() : "1";
                }
                if (index == "首页")
                {
                    index = "1";
                }
                if (index == "尾页")
                {
                    index = pageManage.ItemSourceCount.ToString();
                }
            }
        }

        private void pageManage2_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (e.OriginalSource.ToString().Contains("CustomRadioButton"))
            {
                CustomRadioButton button = e.OriginalSource as CustomRadioButton;
                string index = button.Content.ToString();
                if (index == ">")
                {
                    //index = pageManage.Content.ToString();
                    index = pageManage.CurrentIndex < pageManage.ItemSourceCount ? (pageManage.CurrentIndex + 1).ToString() : pageManage.CurrentIndex.ToString();
                }
                if (index == "<")
                {
                    index = pageManage.CurrentIndex > 1 ? (pageManage.CurrentIndex - 1).ToString() : "1";
                }
                if (index == "首页")
                {
                    index = "1";
                }
                if (index == "尾页")
                {
                    index = pageManage.ItemSourceCount.ToString();
                }
            }
        }

        void PART_CoursesItem_Cloud_Loaded(object sender, RoutedEventArgs e)
        {
            var _rb = (RadioButton)sender;
            _rb.Checked += PART_CoursesItem_Cloud_Checked;

            if (!_is_loaded_first)
            {
                _is_loaded_first = true;
                _rb.IsChecked = new bool?(true);
            }
        }

        void PART_CoursesItem_Cloud_Checked(object sender, RoutedEventArgs e)
        {
            ResourceCenterModel _model = (ResourceCenterModel)((RadioButton)sender).DataContext;
            _current_id = _model.id;
            LoadResource(1);
            pageManage.ItemSourceCount = resourceViewModel.pageCount;
            pageManage.UpdataPages();
        }

        void PART_Cloud_Resource_Item_Loaded(object sender, RoutedEventArgs e)
        {
            PART_Resource_LessionItem _item = (PART_Resource_LessionItem)sender;
            _item.CollectCallback = Collect;
            _item.OpenResourceCallback = OpenResource;
        }

        void PART_Cloud_Resource_Item_Loaded2(object sender, RoutedEventArgs e)
        {
            PART_Resource_LessionItem _item = (PART_Resource_LessionItem)sender;
            _item.CollectCallback = _id =>
            {
                if (resourceViewModel.Collect(_id))
                {
                    LoadMyZiYuan();
                }
                return false;
            };
            _item.OpenResourceCallback = OpenResource2;
        }

        void LoadResource(int _page_index)
        {
            _list_room_cloud = resourceViewModel.GetKindItemList(_current_id, _page_index);
            PART_LessionItems.ItemsSource = _list_room_cloud;
        }

        bool Collect(int _id)
        {
            try
            {
                return resourceViewModel.Collect(_id);
            }
            catch (Exception ex)
            {
                App.Log(ex);
            }
            return false;
        }

        void OpenResource(respResourcekinditemModel _model)
        {
            try
            {
                var _model_res = resourceViewModel.GetCategoryCourseList(_model.id);
                if (_model_res != null)
                {
                    //this.PART_Media_Host.CloseCallback = _is_changed =>
                    //{
                    //    if (_is_changed)
                    //    {
                    //        LoadResource(pageManage.CurrentIndex);
                    //    }
                    //};
                    //this.PART_Media_Host.Load(_model_res);

                    Win_ResourceCenter _win = new Win_ResourceCenter();
                    _win.Owner = Application.Current.MainWindow;
                    _win.Load(_model_res);
                    _win.CollectCallback = Collect;
                    _win.ShowDialog();

                    if (_win.IsCollectStatusChanged)
                    {
                        LoadResource(pageManage.CurrentIndex);
                    }
                }
            }
            catch (Exception ex)
            {
                App.Log(ex);
            }
        }

        void OpenResource2(respResourcekinditemModel _model)
        {
            try
            {
                var _model_res = resourceViewModel.GetCategoryCourseList(_model.id);
                if (_model_res != null)
                {
                    //this.PART_Media_Host.CloseCallback = _is_changed =>
                    //{
                    //    if(_is_changed)
                    //    {
                    //        LoadMyZiYuan();
                    //    }
                    //};
                    //this.PART_Media_Host.Load(_model_res);

                    Win_ResourceCenter _win = new Win_ResourceCenter();
                    _win.Owner = Application.Current.MainWindow;
                    _win.Load(_model_res);
                    _win.CollectCallback = Collect;
                    _win.ShowDialog();

                    if(_win.IsCollectStatusChanged)
                    {
                        LoadMyZiYuan();
                    }
                }
            }
            catch (Exception ex)
            {
                App.Log(ex);
            }
        }

        void LoadMyZiYuan(int _page_index = 1)
        {
            var _list_item = new List<respResourcekinditemModel>();
            var _list = resourceViewModel.GetMySourceListList(_page_index);
            if (_list != null && _list.Count > 0)
            {
                foreach (var _item in _list)
                {
                    if (_item.ziyuan == null)
                        continue;

                    var _model = new respResourcekinditemModel
                    {
                        id = _item.ziyuan.id,
                        resources_kind_id = _item.ziyuan.resources_kind_id,
                        kind_item_name = _item.ziyuan.kind_item_name,
                        updatetime = _item.ziyuan.updatetime,
                        deletetime = _item.ziyuan.deletetime,
                        image = _item.ziyuan.image,
                        shownum = _item.ziyuan.shownum,
                        collection = 1,
                        collectionnum = _item.collectionnum,
                        kind_item_id = _item.ziyuan.kind_item_id,
                    };
                    _list_item.Add(_model);
                }
            }
            pageManage2.ItemSourceCount = resourceViewModel.pageCount2;
            pageManage2.UpdataPages();
            PART_LessionItems2.ItemsSource = _list_item;
        }

        int _current_id;
        bool _is_loaded_first;
        List<respResourcekinditemModel> _list_room_cloud;
    }
}
